﻿using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.Class;
using System;
using System.Linq;
using System.Web.Mvc;
using Resources;
using KPIS.GERP.WEB.GIMS.Class;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
	public class MasUnitController : Controller
	{
		public ActionResult Index()
		{
			var service = new IcmMasterService.IcmMasterServiceClient();
			return View(service.SelectUnit(new MasUnit {RecordStatus = "!D"}, null));
		}

        public ActionResult TestIndex()
        {
            return View();
        }
        public JsonResult Data(string sEcho, int iDisplayStart, int iDisplayLength)
        {
            var service = new IcmMasterService.IcmMasterServiceClient();
            var model = service.SelectUnit(new MasUnit { RecordStatus = "!D" }, null);

            //Extract only current page
            var modelSkiped = model.Skip(iDisplayStart).Take(iDisplayLength);
            var result = from c in modelSkiped
                         select new[] {
                            GridHelper.CheckBox(c.UnitId),
                            c.Name,
                            c.Sign,
                            c.Descr,
                            GridHelper.InLineButton(Url.Action("Details") + "/" + c.UnitId, Url.Action("Edit") + "/" + c.UnitId, Url.Action("Delete") + "/" + c.UnitId)
                        };
            return Json(new
            {
                sEcho = sEcho,
                iTotalRecords = model.Count(),
                iTotalDisplayRecords = model.Count(),
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

		public ActionResult Details(int id)
		{
			var service = new IcmMasterService.IcmMasterServiceClient();
			var model = service.SelectUnit(new MasUnit {UnitId = id}, null).FirstOrDefault();
			return View(model);
		}

		public ActionResult Create()
		{
			return View();
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Create(MasUnit model)
		{
			try
			{
				if (ModelState.IsValid)
				{
					var service = new IcmMasterService.IcmMasterServiceClient();
					var id = service.InsertUnit(model, Class.GimsMembership.CurrentEmpId);
					return RedirectToAction("Details", new {id});
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return View(model);
		}

		public ActionResult Edit(int id)
		{
			var service = new IcmMasterService.IcmMasterServiceClient();
			var model = service.SelectUnit(new MasUnit {UnitId = id}, null).FirstOrDefault();
			return View(model);
		}

		[HttpPost, ValidateAntiForgeryToken]
		public ActionResult Edit(int id, MasUnit model)
		{
			try
			{
				if (ModelState.IsValid)
				{
					var service = new IcmMasterService.IcmMasterServiceClient();
					service.UpdateUnit(model, Class.GimsMembership.CurrentEmpId);

					return RedirectToAction("Details", new {id});
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}

			return View(model);
		}

		public ActionResult Delete(int id)
		{
			try
			{
				var service = new IcmMasterService.IcmMasterServiceClient();
				service.DeleteUnit(id, Class.GimsMembership.CurrentEmpId);
				service.Close();
				return RedirectToAction("Index");
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		
		[HttpPost, ValidateAntiForgeryToken]
		public ActionResult DeleteAll(int[] deleteIds)
		{
			if (deleteIds == null)
				return Json(new { success = true });

			try
			{
				var service = new IcmMasterService.IcmMasterServiceClient();
				foreach (int delete in deleteIds)
				{
					service.DeleteUnit(delete, Class.GimsMembership.CurrentEmpId);
				}

				ModelState.AddModelError(ErrorLocalization.Error, string.Format("ลบข้อมูลไปทั้งหมด {0} รายการ", deleteIds.Length.ToString("#,##0")));
				return Json(new {success = true});
			}
			catch (Exception ex)
			{
				ModelState.AddModelError(ErrorLocalization.Error, ErrorLocalization.ErrorWhileDeleteData);
				GerpLog.Error(ErrorLocalization.ErrorWhileDeleteData + ", " + ex.Message);
				return Json(new {success = false});
			}
		}
	}
}